package com.qf.Servlet;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class BaseServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) {
        resp.setCharacterEncoding("UTF-8");
        //获取前端传递的数据
        String mark = req.getParameter("mark");
        if (mark == null || "".equals(mark)) {
            throw new RuntimeException("方法名不存在");
        }
        //获取class对象
        try {
            Class<? extends BaseServlet> cla = this.getClass();
            Method m = cla.getMethod(mark, HttpServletRequest.class, HttpServletResponse.class);
            //调用方法
            m.invoke(this, req, resp);
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
    }
}
